<?php

namespace app\langjinmobile\controller;

use app\common\model\Margin;
use app\common\model\Reward;
use app\common\model\UserLevel;
use think\Controller;
use think\Request;
use think\facade\Env;
use app\common\model\Money;
use app\common\model\WalletLog;
use app\common\model\IntegralLog ;
use app\common\model\User as userModel;
use app\common\model\Banner as bannerModel;
use app\common\model\Config as configModel;
use app\backend\model\AdminUser as adminuserModel;
use app\common\model\Sendmsg as SendmsgModel;
use app\common\model\Area as AreaModel;
use app\common\model\Assets as AssetsModel;
use think\Validate;
use think\Db;


class Common extends Controller
{
    //申请社区中心
    /**
    **type 1申请节点 2申请超级节点 3申请社区
     **/
    public function application(Request $request)
    {
        if ($request->isPost()) {
            $userid = session('userid');
            $type = $request->param('type');
            //查询当前等级是否可以升级
            $user_grade = UserLevel::where('userid', $userid)->value('user_grade');
            $level_grade = UserLevel::where('userid', $userid)->value('level_grade');
            if ($level_grade >= $type) {
                $status = [
                    '1' => '节点',
                    '2' => '超级节点',
                    '3' => '社区',
                ];
                return ['code' => 0, 'msg' => '您已经是' . $status[$level_grade]];
            }
            $wallet_type = ['1' => 5,
                '2' => 6,
                '3' => 7,
            ];
            $integral_type = ['1' => 6,
                '2' => 7,
                '3' => 8,
            ];
            switch ($type) {
                case '1':
                    if ($user_grade < 3) {
                        return ['code' => 0, 'msg' => '您的等级不足，三星会员才能申请'];
                    } else {
                        $count = Margin::where(['userid'=>$userid,'status'=>0,'type'=>1])->count();
                        if($count){
                            return ['code' => 0, 'msg' => '请勿重复提交'];
                        }
                        //获取配置
                        $config = Reward::where('id', 7)->find();
                        $money = Money::where('userid', $userid)->find();
                        if ($money['balance'] < $config['tip']) {
                            return ['code' => 0, 'msg' => 'GMOB不足'];
                        }
                        if ($money['integral'] < $config['condition']) {
                            return ['code' => 0, 'msg' => 'GMOD不足'];
                        }
                        //扣除GMOB和GMOD，添加申请记录
                        //写入GMOB记录表
                        $data['userid'] = $userid;
                        $data['money'] = $config['tip'];
                        $data['before_wallet'] = $money['balance'];
                        $data['now_wallet'] = $money['balance'] - $config['tip'];
                        $data['type'] = $wallet_type[$type];
                        $data['create_time'] = time();
                        $result = WalletLog::insert($data);
                        //增加GMOD
                        $data1['userid'] = $userid;
                        $data1['num'] = $config['condition'];
                        $data1['type'] = $integral_type[$type];
                        $data1['before_integral'] = $money['integral'];//交易之前的GMOD
                        $data1['now_integral'] = $money['integral'] - $config['condition'];//交易之后的GMOD
                        $data1['create_time'] = time();
                        $res1 = IntegralLog::insert($data1);
                        //添加申请记录
                        $data3['userid'] = $userid;
                        $data3['balance'] = $config['tip'];
                        $data3['integral'] = $config['condition'];
                        $data3['status'] = 0;
                        $data3['type'] = $type;
                        $data3['create_time'] = time();
                        $res2 = Margin::insert($data3);
                        if ($data && $data1 && $res2) {
                            Money::where('userid', $userid)->update(['balance' => $data['now_wallet'], 'integral' => $data1['now_integral']]);
                            return ['code' => 1, 'msg' => '申请成功'];
                        } else {
                            return ['code' => 0, 'msg' => '申请失败'];
                        }
                    }
                    break;
                case '2':
                    if ($user_grade < 6) {
                        return ['code' => 0, 'msg' => '您的等级不足，六星会员才能申请'];
                    } else {
                        $count = Margin::where(['userid'=>$userid,'status'=>0,'type'=>2])->count();
                        if($count){
                            return ['code' => 0, 'msg' => '请勿重复提交'];
                        }
                        //获取配置
                        $config = Reward::where('id', 8)->find();
                        $money = Money::where('userid', $userid)->find();
                        if ($money['balance'] < $config['tip']) {
                            return ['code' => 0, 'msg' => 'GMOB不足'];
                        }
                        if ($money['integral'] < $config['condition']) {
                            return ['code' => 0, 'msg' => 'GMOD不足'];
                        }
                        //扣除GMOB和GMOD，添加申请记录
                        //写入GMOB记录表
                        $data['userid'] = $userid;
                        $data['money'] = $config['tip'];
                        $data['before_wallet'] = $money['balance'];
                        $data['now_wallet'] = $money['balance'] - $config['tip'];
                        $data['type'] = $wallet_type[$type];
                        $data['create_time'] = time();
                        $result = WalletLog::insert($data);
                        //增加GMOD
                        $data1['userid'] = $userid;
                        $data1['num'] = $config['condition'];
                        $data1['type'] = $integral_type[$type];
                        $data1['before_integral'] = $money['integral'];//交易之前的GMOD
                        $data1['now_integral'] = $money['integral'] - $config['condition'];//交易之后的GMOD
                        $data1['create_time'] = time();
                        $res1 = IntegralLog::insert($data1);
                        //添加申请记录
                        $data3['userid'] = $userid;
                        $data3['balance'] = $config['tip'];
                        $data3['integral'] = $config['condition'];
                        $data3['status'] = 0;
                        $data3['type'] = $type;
                        $data3['create_time'] = time();
                        $res2 = Margin::insert($data3);
                        if ($data && $data1 && $res2) {
                            Money::where('userid', $userid)->update(['balance' => $data['now_wallet'], 'integral' => $data1['now_integral']]);
                            return ['code' => 1, 'msg' => '申请成功'];
                        } else {
                            return ['code' => 0, 'msg' => '申请失败'];
                        }
                    }
                    break;
                case '3':
                    if ($user_grade < 9) {
                        return ['code' => 0, 'msg' => '您的等级不足，九星会员才能申请'];
                    } else {
                        $count = Margin::where(['userid'=>$userid,'status'=>0,'type'=>3])->count();
                        if($count){
                            return ['code' => 0, 'msg' => '请勿重复提交'];
                        }
                        //获取配置
                        $config = Reward::where('id', 9)->find();
                        $money = Money::where('userid', $userid)->find();
                        if ($money['balance'] < $config['tip']) {
                            return ['code' => 0, 'msg' => 'GMOB不足'];
                        }
                        if ($money['integral'] < $config['condition']) {
                            return ['code' => 0, 'msg' => 'GMOD不足'];
                        }
                        //扣除GMOB和GMOD，添加申请记录
                        //写入GMOB记录表
                        $data['userid'] = $userid;
                        $data['money'] = $config['tip'];
                        $data['before_wallet'] = $money['balance'];
                        $data['now_wallet'] = $money['balance'] - $config['tip'];
                        $data['type'] = $wallet_type[$type];
                        $data['create_time'] = time();
                        $result = WalletLog::insert($data);
                        //增加GMOD
                        $data1['userid'] = $userid;
                        $data1['num'] = $config['condition'];
                        $data1['type'] = $integral_type[$type];
                        $data1['before_integral'] = $money['integral'];//交易之前的GMOD
                        $data1['now_integral'] = $money['integral'] - $config['condition'];//交易之后的GMOD
                        $data1['create_time'] = time();
                        $res1 = IntegralLog::insert($data1);
                        //添加申请记录
                        $data3['userid'] = $userid;
                        $data3['balance'] = $config['tip'];
                        $data3['integral'] = $config['condition'];
                        $data3['status'] = 0;
                        $data3['type'] = $type;
                        $data3['create_time'] = time();
                        $res2 = Margin::insert($data3);
                        if ($data && $data1 && $res2) {
                            Money::where('userid', $userid)->update(['balance' => $data['now_wallet'], 'integral' => $data1['now_integral']]);
                            return ['code' => 1, 'msg' => '申请成功'];
                        } else {
                            return ['code' => 0, 'msg' => '申请失败'];
                        }
                    }
                    break;
                default:
                    return ['code' => 0, 'msg' => '申请失败'];
            }
        }
    }
    
}
